﻿using Accessibility;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace iseeqq
{
    class QqWindowHelper
    {
        public const int WM_SETTEXT = 0x000C;
        public const int WM_CLICK = 0x00F5;

        public const int CHILDID_SELF = 0;
        public const int CHILDID_1 = 1;
        public const int OBJID_CLIENT = -4;

        [DllImport("Oleacc.dll")]
        public static extern int AccessibleObjectFromWindow(
        IntPtr hwnd,
        int dwObjectID,
        ref Guid refID,
        ref IAccessible ppvObject);


        [DllImport("Oleacc.dll")]
        public static extern int AccessibleChildren(
        Accessibility.IAccessible paccContainer,
        int iChildStart,
        int cChildren,
        [Out] object[] rgvarChildren,
        out int pcObtained);





        /// <summary>
        /// QQ聊天窗口 右键句柄
        /// </summary>
        private static int[] winMouseRight = new int[] { 3 };
        /// <summary>
        ///检查窗口是否为多彩气泡，否则取消设置，
        /// </summary>
        public static void checkTextStype(IntPtr handle)
        {
            Win32API.RECT rect = new Win32API.RECT();
            Win32API.GetWindowRect(handle, ref rect);
            Win32API.SetCursorPos(rect.Left + 400, rect.Top + 250);

            //右键
            Win32API.mouse_event(Win32API.MOUSEEVENTF_RIGHTDOWN | Win32API.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
            Thread.Sleep(500);
            handle = Win32API.WindowFromPoint(new Point(rect.Left + 430, rect.Top + 280));

            Accessibility.IAccessible IACurrent = null;
            AccessibleObjectFromWindow(handle, (int)OBJID_CLIENT, ref guidCOM, ref IACurrent);
            IACurrent = (IAccessible)IACurrent.accParent;
            IAccessible inputBox = GetAccessibleChild(IACurrent, winMouseRight);
            for (int i = 0; i < inputBox.accChildCount; i++)
            {
                string name = inputBox.get_accName(i);
                int state = (int)inputBox.get_accState(i);
                if (name == "使用多彩气泡" && state == 16) // 取消多彩气泡打钩了
                {
                    int l, t, r, b;
                    inputBox.accLocation(out l, out  t, out  r, out  b, i);

                    Win32API.SetCursorPos(l + 10, t + 5);
                    Win32API.mouse_event(Win32API.MOUSEEVENTF_LEFTDOWN | Win32API.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    Thread.Sleep(100);
                    return;
                }
            }
        }

        [DllImport("Oleacc.dll")]
        public static extern int WindowFromAccessibleObject(
            IAccessible pacc,
            out IntPtr phwnd);


        /// <summary>
        /// QQ聊天窗口类型
        /// </summary>
        private static int[] winChatType = new int[] { 3, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
        public static int getChatType(IntPtr imWindowHwnd)
        {
            if (imWindowHwnd == IntPtr.Zero) return -1;

            Accessibility.IAccessible IACurrent = null;
            AccessibleObjectFromWindow(imWindowHwnd, (int)OBJID_CLIENT, ref guidCOM, ref IACurrent);
            IACurrent = (IAccessible)IACurrent.accParent;
            IAccessible inputBox = GetAccessibleChild(IACurrent, winChatType);

            string name = inputBox.get_accName(2);
            switch (name)
            {
                case "多人会话":
                    return 0; //个人
                case "设置":
                    return 1;//群聊
                case null:
                    return 2;//讨论组
                default:
                    return -1;
            }
        }


        private static Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71);

        /// <summary>
        /// QQ聊天窗口的内容
        /// </summary>
        private static int[] winChatContext = new int[] { 3, 1, 2, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0 };
        public static IAccessible getChatContext(IntPtr imWindowHwnd)
        {
            Accessibility.IAccessible IACurrent = null;
            AccessibleObjectFromWindow(imWindowHwnd, (int)OBJID_CLIENT, ref guidCOM, ref IACurrent);
            IACurrent = (IAccessible)IACurrent.accParent;
            IAccessible inputBox = GetAccessibleChild(IACurrent, winChatContext);
            return inputBox;
        }

        private static IAccessible[] GetAccessibleChildren(IAccessible paccContainer)
        {
            IAccessible[] rgvarChildren = new IAccessible[paccContainer.accChildCount];
            int pcObtained;
            AccessibleChildren(paccContainer, 0, paccContainer.accChildCount, rgvarChildren, out pcObtained);
            return rgvarChildren;

        }

        private static IAccessible GetAccessibleChild(IAccessible paccContainer, int[] array)
        {
            if (array.Length > 0)
            {
                IAccessible result = GetAccessibleChildren(paccContainer)[array[0]];
                int[] array_1 = new int[array.Length - 1];
                for (int i = 0; i < array.Length - 1; i++)
                {
                    array_1[i] = array[i + 1];
                }
                return GetAccessibleChild(result, array_1);
            }
            else
            {
                return paccContainer;
            }
        }
    }
}
